package com.haogu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.haogu.entity.PTask;
import com.haogu.entity.Task;
import com.haogu.entity.User;
import com.haogu.entity.VTask;
import com.haogu.mapper.TaskMapper;
import com.haogu.mapper.UserMapper;
import com.haogu.service.TaskService;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TaskServiceImpl implements TaskService {

    @Resource
    private TaskMapper taskMapper;
    @Resource
    private UserMapper userMapper;
    /**
     *添加任务
     */
    public Map addTask(Task task, User user){
        task.setCreatorUserId(user.getId());
        int insert = taskMapper.insert(task);
        Map map = new HashMap();
        if(insert == 0){
            map.put("success",false);
            map.put("message","添加失败");
        }else{
            map.put("success",true);
            map.put("message","添加成功");
        }
        return map;
    }

    /**
     * 分页展示数据
     * @return
     */
    @Override
    public Map listTask(PTask p) {
        Map map = new HashMap();
        //查询分页数据
        List<VTask> vTasks = taskMapper.selectAllTaskByPage((p.getPage() - 1) * p.getRows(),p);
        //查询行数
        QueryWrapper<Task> qw = new QueryWrapper<>();
        qw.eq("delete_status",0);
        Integer integer = taskMapper.selectCount(qw);
        //将数据放入map返回
        map.put("totalRows",integer);
        map.put("result",vTasks);
        Map map1 = new HashMap();
        map1.put("data",map);
        return map1;
    }
    /**
     * 查看
     */
    public void seeTask(Integer id, Model model){
        Task task = taskMapper.selectById(id);
        User createUser = userMapper.selectUserByUserId(task.getCreatorUserId());
        User ownerUser = userMapper.selectUserByUserId(task.getOwnerUserId());
        model.addAttribute("task",task);
        model.addAttribute("createUser",createUser);
        model.addAttribute("ownerUser",ownerUser);
    }
    

    /**
     * 删除，批量的
     */
    public Map deleteTask(String ids,User user){
        //对传过来的ids进行操作
        String[] split = ids.split(",");
        System.out.println(ids);
        System.out.println(split);
        boolean flag = true;
        for(int i = 0; i < split.length;i++){

            int i1 = taskMapper.deleteById(Integer.parseInt(split[i]));
//            boolean b = leadsService.deleteLeadsById(Integer.parseInt(split[i]));
            if(i1==0){
                taskMapper.updateDeleteUserIdAndDeleteTime(user.getId(),Integer.parseInt(split[i]));
                flag = false;
            }
        }
        Map map = new HashMap();
        if(flag){
            map.put("success",true);
            map.put("message","删除成功");
        }else{
            map.put("success",false);
            map.put("message","删除失败");
        }
        return map;
    }

    /**
     * 修改
     * @param id
     * @param model
     */
    @Override
    public void toUpdateTask(Integer id, Model model) {
        Task task = taskMapper.selectById(id);
        List<User> users = userMapper.selectList(null);
        model.addAttribute("task",task);
        model.addAttribute("users",users);
    }

    @Override
    public Map updateTask(Task task) {
        int i = taskMapper.updateById(task);
        Map map = new HashMap();
        if(i==0){
            map.put("success",false);
            map.put("message","修改失败");
        }else{
            map.put("success",true);
            map.put("message","修改成功");
        }
        return map;
    }

    /**
     * 关闭任务
     */
    public Map closeTask(Integer id){
        Map map = new HashMap();
        int i = taskMapper.updateIsCloseAndStatus(id);
        System.out.println("11111"+i);
        if(i==0){
            map.put("success",false);
            map.put("message","关闭失败");
        }else{
            map.put("success",true);
            map.put("message","关闭成功");
        }
        return map;
    }


    /**
     * ------------------------------------------------------------------------------------------
     * 回收站展示
     */
    /**
     * 分页展示数据
     * @return
     */
    @Override
    public Map listTaskHuishou(Integer page,Integer rows,String subject,Integer select) {
        Map map = new HashMap();
        //查询分页数据

        List<VTask> vTasks = taskMapper.selectAllTaskByPageHuishou((page-1)*rows,rows,subject,select);
        //查询行数
        QueryWrapper<Task> qw = new QueryWrapper<>();
        qw.eq("delete_status",1);
        Integer integer = taskMapper.selectCount(qw);
        //将数据放入map返回
        map.put("totalRows",integer);
        map.put("result",vTasks);
        Map map1 = new HashMap();
        map1.put("data",map);
        return map1;
    }

    /**
     * 删除回收站中的一条数据
     * @param id
     * @return
     */
    @Override
    public Map toDeleteTaskHuishou(Integer id) {
        int i = taskMapper.toDeleteTaskHuishou(id);
        Map map = new HashMap();
        if(i==1){
            map.put("success",true);
            map.put("message","删除成功");
        }else{
            map.put("success",false);
            map.put("message","删除失败");
        }
        return map;
    }

    /**
     * 清空所有
     * @return
     */
    public Map toClearAll(){
        int i = taskMapper.toClearAll();
        Map map = new HashMap();
        if(i>0){
            map.put("success",true);
            map.put("message","删除成功");
        }else{
            map.put("success",false);
            map.put("message","删除失败");
        }
        return map;
    }

    /**
     * 恢复
     * @param id
     * @return
     */
    @Override
    public Map huiFuTask(Integer id) {
        int i = taskMapper.huiFuTask(id);
        Map map = new HashMap();
        if(i>0){
            map.put("success",true);
            map.put("message","恢复成功");
        }else{
            map.put("success",false);
            map.put("message","恢复失败");
        }
        return map;
    }
}
